package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 破晓属性匹配
 * @author ruoyi
 * @data 2024/7/5
 * @apiNote
 */
@Service
public class JINGMENGYOUAttr implements AdapterAttr {
    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        List<GamesRes> resultList = new ArrayList<>();
        String allGame = getAllGame(salePartner);
        String data = JSONObject.parseObject(allGame).getString("data");
        String list = JSONObject.parseObject(data).getString("list");
        List<JSONObject> array = JSONObject.parseArray(list, JSONObject.class);
        for (JSONObject jsonObject : array) {
            GamesRes res = new GamesRes();
            res.setId(jsonObject.getString("id"));
            res.setGameName(jsonObject.getString("name"));
            resultList.add(res);
        }
        return resultList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> header = new HashMap<>();
        header.put("XX-Token", "bd0a84c51ca82e50de7cc2d03b487ba1eb25f2b29a180e2fb9d2c176b9012574");
        header.put("XX-Device-Type", "pc");
        header.put("XX-App", "trading");
        header.put("XX-Subsite", "0");
        header.put("Content-Type", "application/json");

        params.put("id", gameId);
        String response = HttpUtils.sendPost("https://api.jingmengyou.com/api/game/Index/search", JSONObject.toJSONString(params), "application/json");
        String data1 = JSONObject.parseObject(response).getString("data");
        String game = JSONObject.parseObject(data1).getString("game");
        if(game.contains("英雄联盟")||game.contains("穿越火线CF")){
            JSONObject jsonObject = JSONObject.parseArray(game, JSONObject.class).get(0);
            game = jsonObject.getString("children");
        }
        List<JSONObject> data = JSONObject.parseArray(game, JSONObject.class);
        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            String children = datum.getString("children");
            List<JSONObject> childrens = JSONObject.parseArray(children, JSONObject.class);
            for (JSONObject datum2 : childrens) {
                InterServerRes interServer = new InterServerRes();
                interServer.setId(datum2.getString("id"));
                interServer.setName(datum2.getString("name"));
                String servers = datum2.getString("children");
                if(StringUtils.isNotEmpty(servers)){
                    List<InterServerRes> interServerRes1 = new ArrayList<>();
                    List<JSONObject> data2 = JSONObject.parseArray(servers, JSONObject.class);
                    for(JSONObject datum3 : data2){
                        InterServerRes interServer1 = new InterServerRes();
                        String id1 = datum3.getString("id");
                        String title1 = datum3.getString("name");
                        interServer1.setId(id1);
                        interServer1.setName(title1);
                        String servers2 = datum3.getString("children");
                        if(StringUtils.isNotEmpty(servers2)){
                            List<InterServerRes> interServerRes2 = new ArrayList<>();
                            List<JSONObject> data3 = JSONObject.parseArray(servers2, JSONObject.class);
                            for(JSONObject datum4 : data3){
                                InterServerRes interServer2 = new InterServerRes();
                                interServer2.setId(datum4.getString("id"));
                                interServer2.setName(datum4.getString("name"));
                                interServerRes2.add(interServer2);
                                interServer1.setInterServerRes(interServerRes2);
                            }
                        }
                        interServerRes1.add(interServer1);
                    }
                    interServer.setInterServerRes(interServerRes1);
                }
                interServerRes.add(interServer);
            }
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> header = new HashMap<>();
        header.put("XX-Token", "bd0a84c51ca82e50de7cc2d03b487ba1eb25f2b29a180e2fb9d2c176b9012574");
        header.put("XX-Device-Type", "pc");
        header.put("XX-App", "trading");
        header.put("XX-Subsite", "0");
        header.put("Content-Type", "application/json");

        params.put("id", gameId);
        String response = HttpUtils.sendPost("https://api.jingmengyou.com/api/game/Index/search", JSONObject.toJSONString(params), "application/json");
        String data1 = JSONObject.parseObject(response).getString("data");
        String types = JSONObject.parseObject(data1).getString("type");
        Object o = JSONArray.parseArray(types).get(0);
        String additionalFields = JSONObject.parseObject(o.toString()).getString("additionalFields");
        JSONObject obj = JSONObject.parseObject(additionalFields);
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        List<JSONObject> collect = new ArrayList<>();

        for (Map.Entry<String, Object> entry : obj.entrySet()) {
            JSONObject jsonObject = new JSONObject();
            String key = entry.getKey();
            Object value = entry.getValue();
            String string = JSONObject.parseObject(value.toString()).getString("type");
            if (Objects.equals("1",type)){
                if(string.equals("radio")){
                    jsonObject.put("key",key);
                    jsonObject.put("value",value);
                    collect.add(jsonObject);
                }
            }else if  (Objects.equals("2",type)){
                if(string.equals("checkbox")){
                    jsonObject.put("key",key);
                    jsonObject.put("value",value);
                    collect.add(jsonObject);
                }
            }else if (Objects.equals("3",type)){
                if(string.equals("input")){
                    jsonObject.put("key",key);
                    jsonObject.put("value",value);
                    collect.add(jsonObject);
                }
            }
        }
        for (JSONObject object : collect) {
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setId(object.getString("key"));
            interAttrRes.setName(JSONObject.parseObject(object.getString("value")).getString("name"));
            String options = JSONObject.parseObject(object.getString("value")).getString("options");
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            List<String> list = JSONObject.parseArray(options, String.class);
            if(!CollectionUtils.isEmpty(list)) {
                for (String s : list) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setId(s);
                    interAttrRes1.setName(s);
                    interAttrResList1.add(interAttrRes1);
                }
            }
            interAttrRes.setInterServerRes(interAttrResList1);
            interAttrResList.add(interAttrRes);
        }

        return interAttrResList;
    }

    public static void main(String[] args) {
//        getAtta("10");
//        getAllGame();
//        getAllServer("11");
    }

    static String getAllServer(String gameId) {
        String url = "http://mhh.kaigewangyou.com/api/api.php?pg=getAllServer";

        Map<String, Object> params = new TreeMap<>();
        params.put("pg", "getAllServer");
        params.put("gameId", gameId);
        String str = getCover1(params) + "key=" + "x8logf11d6o898l83botgg105c9ky83rjoi";
        String sign = DigestUtils.md5Hex(str).toUpperCase();
        params.put("signKey", sign);
        String response = HttpUtils.doPostForm(url, params, null);
        System.out.println(JSONObject.parseObject(response));
        return response;
    }

    static String getAtta(String gameId, SalePartner salePartner) {
        String url = "http://mhh.kaigewangyou.com/api/api.php?pg=getGameId";

        Map<String, Object> params = new TreeMap<>();
        params.put("pg", "getGameId");
        params.put("gameId", gameId);
        String str = getCover1(params) + "key=" + salePartner.getSaleKey();
        String sign = DigestUtils.md5Hex(str).toUpperCase();
        params.put("signKey", sign);
        String response = HttpUtils.doPostForm(url, params, null);
        System.out.println(JSONObject.parseObject(response));
        return response;
    }

    static String getAllGame(SalePartner salePartner) {
        Map<String, String> params = new HashMap<>();
        params.put("device", "-1");
        String response = HttpRequest.post("https://api.jingmengyou.com/api/game/Index/games").header("XX-Token", "bd0a84c51ca82e50de7cc2d03b487ba1eb25f2b29a180e2fb9d2c176b9012574").header("XX-Device-Type", "pc")
                .header("XX-App", "trading").header("XX-Subsite", "0").body(JSONObject.toJSONString(params)).execute().body();
        return response;
    }

    public static String getCover1(Map<String, Object> params) {
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            resut.append(key + "=" + params.get(key) + "&");
        }
        return resut.toString();
    }
}
